package com.example.video.video_server.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.video.common.util.ResponseWrap;
import com.example.video.common.util.ResponseWrapUtil;
import com.example.video.common.util.SysUserUtils;
import com.example.video.video_server.entity.VideoInfo;
import com.example.video.video_server.entity.vo.VideoDetailsVo;
import com.example.video.video_server.service.IVideoInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 视频基本信息表 前端控制器
 * </p>
 *
 * @author yj
 */
@RestController
@RequestMapping("/video/info")
public class VideoInfoController {

    @Autowired
    private IVideoInfoService iVideoInfoService;

    @PostMapping("/add")
    @ApiOperation(value = "新增视频")
    public ResponseWrap<VideoInfo> add(@RequestBody VideoInfo videoInfo){
        Integer id = SysUserUtils.getUserDetails().getSysUser().getId();
        videoInfo.setCreatedId(id);
        boolean save = iVideoInfoService.save(videoInfo);
        return ResponseWrapUtil.success(videoInfo);
    }

    @PostMapping ("/update")
    @ApiOperation(value = "修改视频")
    public ResponseWrap<VideoInfo> update(@RequestBody VideoInfo videoInfo){
        boolean update = iVideoInfoService.updateById(videoInfo);
        return ResponseWrapUtil.success();
    }

    @GetMapping ("/getById")
    @ApiOperation(value = "通过id获取")
    public ResponseWrap<VideoInfo> getById(Integer id){
        VideoInfo byId = iVideoInfoService.getById(id);
        return ResponseWrapUtil.success(byId);
    }

    @GetMapping ("/getDetailsById")
    @ApiOperation(value = "通过id获取视频详情")
    public ResponseWrap<VideoDetailsVo> getDetailsById(Integer id){
        VideoDetailsVo videoDetailsVo = iVideoInfoService.getDetailsById(id);
        return ResponseWrapUtil.success(videoDetailsVo);
    }

    @DeleteMapping("/del")
    @ApiOperation(value = "删除视频")
    public ResponseWrap<VideoInfo> del(Integer id){
        boolean b = iVideoInfoService.del(id);
        return ResponseWrapUtil.success();
    }

    @GetMapping("/getPaginationList")
    @ApiOperation(value = "条件分页查询视频列表")
    public ResponseWrap<Page> getPaginationList(Integer pageSize, Integer pageIndex, VideoInfo videoInfo){
        Page page = iVideoInfoService.page(new Page(pageIndex, pageSize),new QueryWrapper<>(videoInfo));
        return ResponseWrapUtil.success(page);
    }

    @GetMapping("/getInfoByTxUrl")
    @ApiOperation(value = "通过腾讯路径获取视频资源信息")
    public ResponseWrap getInfoByTxUrl(Integer videoDraId, String url){
        List list = iVideoInfoService.getInfoByTxUrl(videoDraId,url);
        return ResponseWrapUtil.success(list);
    }
}

